﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using GriffinWebService.BO;
using GriffinWebService.Entity;
using GriffinWebService.BE;
using System.Text;

namespace GriffinWebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        userBO userBO = new userBO();
        bookmarkBO bookmarkbo = new bookmarkBO();
        EvaluationBO evaluationBO = new EvaluationBO();

        [WebMethod]
        public string helloworld()
        {
            return "helloworld";
        }


        [WebMethod]
        public int register(string username, string password, string email, out string errormsg)
        {
            User user = new User();
            user.Username = username;
            user.Password = password;
            user.Email = email;
            errormsg = string.Empty;
            user = userBO.register(user, out errormsg);
            return user.UserID;
        }

       

        [WebMethod]
        public int login(string username, string password, out string errormsg)
        {
            errormsg = string.Empty;
            return userBO.login(username, password, out errormsg);
        }

        private static string GetJSONBookmarks(List<Bookmark> bookmarklist)
        {
            bool needComma = false;
            StringBuilder sb = new StringBuilder();

            sb.Append("{\"bookmarks\":");
            sb.Append("[");
            foreach (Bookmark bookmark in bookmarklist)
            {
                if (needComma)
                {
                    sb.Append(",");
                }

                sb.Append("{");
                sb.Append("\"Bookmarkname\":");
                sb.Append("\"");
                sb.Append(bookmark.Bookmarkname.ToString().Trim());
                sb.Append("\"");
                sb.Append(",");
                sb.Append("\"Bookmarkurl\":");
                sb.Append("\"");
                sb.Append(bookmark.Bookmarkurl.ToString().Trim());
                sb.Append("\"");
                sb.Append("}");

                needComma = true;
            }
            sb.Append("]");
            sb.Append("}");

            return sb.ToString();
        }

        #region Evaluation

        [WebMethod]
        public string getCateEvaluation( int cateID, out string errormsg)
        {
            errormsg = string.Empty;

            List<Evaluation> evaluationlist = evaluationBO.getCateEvaluation(cateID, out errormsg);

            return GetJSONCateEvaluations(evaluationlist);
        }

        private static string GetJSONCateEvaluations(List<Evaluation> evaluationlist)
        {
            bool needComma = false;
            StringBuilder sb = new StringBuilder();

            sb.Append("{\"evaluation\":");
            sb.Append("[");
            foreach (Evaluation evaluation in evaluationlist)
            {
                if (needComma)
                {
                    sb.Append(",");
                }

                sb.Append("{");
                sb.Append("\"siteid\":");
                sb.Append("\"");
                sb.Append(evaluation.SiteID.ToString().Trim());
                sb.Append("\"");
                sb.Append(",");
                sb.Append("\"evaluationnumber\":");
                sb.Append("\"");
                sb.Append(evaluation.EvaluationNumber.ToString().Trim());
                sb.Append("\"");
                sb.Append("}");

                needComma = true;
            }
            sb.Append("]");
            sb.Append("}");

            return sb.ToString();
        } 

        [WebMethod]
        public decimal getSiteEvaluation(int userID, int siteID, out string errormsg)
        {
            errormsg = string.Empty;
            return evaluationBO.getSiteEvaluation(userID, siteID, out errormsg);
        }

        [WebMethod]
        public int setEvaluation(int userID, int siteID, int cateid, string evaluationNumber, out string errormsg)
        {
            errormsg = string.Empty;
            return evaluationBO.setEvaluation(userID, siteID, cateid,evaluationNumber, out errormsg);
        }

        #endregion

        #region BookMark

        [WebMethod]
        public int addBookmark(string userid, string bookmarkName, string bookmarkUrl, out string errormsg)
        {
            errormsg = string.Empty;

            return bookmarkbo.AddBookmark(userid, bookmarkName, bookmarkUrl);
        }

        [WebMethod]
        public string getBookmarks(string userid, out string errormsg)
        {
            errormsg = string.Empty;

            List<Bookmark> bookmarklist = bookmarkbo.GetBookmarks(userid);

            return GetJSONBookmarks(bookmarklist);
        }

        [WebMethod]
        public int editBookmarks(int userid, string bookmarkName, string bookmarkUrl, out string errormsg)
        {
            errormsg = string.Empty;

            return bookmarkbo.editBookmarks(userid, bookmarkName, bookmarkUrl,out errormsg);
        }

        [WebMethod]
        public int deleteBookmarks(int userid, string bookmarkName, out string errormsg)
        {
            errormsg = string.Empty;

            return bookmarkbo.deleteBookmarks(userid, bookmarkName, out errormsg);
        }

        #endregion
    }
}
